{extend name="../../../view/public/base" /}

{block name="body"}
<div class="bw-container">
    <div class="bw-main">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <!--列表区域-->
            <div class="layui-card">
                <table id="list" lay-filter="listFilter"></table>

                <!--目录-->
                <script type="text/html" id="dir">
                    {{ d.dir }}
                </script>
                <!--名称-->
                <script type="text/html" id="title">
                    {{ d.title }}
                </script>
                <!--描述内容-->
                <script type="text/html" id="describe">
                    {{ d.describe }}
                </script>
                <!--Logo-->
                <script type="text/html" id="logo_image">
                    {{ getImagesHtmlForIndex(d.logo_image) }}
                </script>
                <!--演示二维码-->
                <script type="text/html" id="qrcode_image">
                    {{ getImagesHtmlForIndex(d.qrcode_image) }}
                </script>
                <!--应用截图-->
                <script type="text/html" id="style_images">
                    {{ getImagesHtmlForIndex(d.style_images) }}
                </script>
                <!--销售价-->
                <script type="text/html" id="sell_price">
                    {{ d.sell_price }}
                </script>
                <!--市场价-->
                <script type="text/html" id="market_price">
                    {{ d.market_price }}
                </script>
                <!--版本-->
                <script type="text/html" id="version">
                    {{ d.version }}
                </script>
                <!--小程序模板-->
                <script type="text/html" id="template_id">
                    {{ d.template_id }}
                </script>
                <!--体验天数-->
                <script type="text/html" id="expire_day">
                    {{ d.expire_day }}
                </script>
                <!--应用类型-->
                <script type="text/html" id="types">
                    {{# if (d.types.search('mini_program') != -1) { }}
                    <span class='layui-badge'>小程序</span>
                    {{# } }}
                    {{# if (d.types.search('app') != -1) { }}
                    <span class='layui-badge'>APP</span>
                    {{# } }}
                    {{# if (d.types.search('h5') != -1) { }}
                    <span class='layui-badge'>h5</span>
                    {{# } }}
                    {{# if (d.types.search('official') != -1) { }}
                    <span class='layui-badge'>公众号</span>
                    {{# } }}
                    {{# if (d.types.search('system') != -1) { }}
                    <span class='layui-badge'>系统</span>
                    {{# } }}
                </script>
                <!--平台管理:0=关闭,1=开启-->
                <script type="text/html" id="is_manage">
                    {{# if (d.is_manage == '0') { }}
                    <span style="color: #1E9FFF;">关闭</span>
                    {{# } else if(d.is_manage == '1') { }}
                    <span style="color: #1E9FFF;">开启</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_manage}}</span>
                    {{# } }}
                </script>
                <!--接入方式:0=独立应用,1=开放平台-->
                <script type="text/html" id="is_openapp">
                    {{# if (d.is_openapp == '0') { }}
                    <span style="color: #1E9FFF;">独立应用</span>
                    {{# } else if(d.is_openapp == '1') { }}
                    <span style="color: #1E9FFF;">开放平台</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_openapp}}</span>
                    {{# } }}
                </script>
                <!--微信支付:0=关闭,1=开启-->
                <script type="text/html" id="is_wechat_pay">
                    {{# if (d.is_wechat_pay == '0') { }}
                    <span style="color: #1E9FFF;">关闭</span>
                    {{# } else if(d.is_wechat_pay == '1') { }}
                    <span style="color: #1E9FFF;">开启</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_wechat_pay}}</span>
                    {{# } }}
                </script>
                <!--支付宝支付:0=关闭,1=开启-->
                <script type="text/html" id="is_alipay_pay">
                    {{# if (d.is_alipay_pay == '0') { }}
                    <span style="color: #1E9FFF;">关闭</span>
                    {{# } else if(d.is_alipay_pay == '1') { }}
                    <span style="color: #1E9FFF;">开启</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_alipay_pay}}</span>
                    {{# } }}
                </script>
                <!--排序-->
                <script type="text/html" id="sort">
                    {{ d.sort }}
                </script>
                <!--状态:-1=未安装,0=关闭,1=开启-->
                <script type="text/html" id="status">
                    {{# if (d.status == '-1') { }}
                    <span style="color: #1E9FFF;">未安装</span>
                    {{# } else if(d.status == '0') { }}
                    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-filter="statusChange" lay-text="开启|关闭">
<!--                    <span style="color: #1E9FFF;">关闭</span>-->
                    {{# } else if(d.status == '1') { }}
                    <input type="checkbox" checked name="status" value="{{d.id}}" lay-skin="switch" lay-filter="statusChange" lay-text="开启|关闭">
<!--                    <span style="color: #1E9FFF;">开启</span>-->
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.status}}</span>
                    {{# } }}
                </script>
                <!--安装时间-->
                <script type="text/html" id="create_time">
                    {{ formatDate(d.create_time) }}
                </script>
                <!--更新时间-->
                <script type="text/html" id="update_time">
                    {{ formatDate(d.update_time) }}
                </script>

                <!--状态开关-->
                <script type="text/html" id="listBar">
                    <!--{Auth:node name='/manage/admin/Miniapp/edit'}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
                    {/Auth:node}
                    {Auth:node name='/manage/admin/Miniapp/del'}
                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                    {/Auth:node}-->

                    <!--应用未安装-->
                    {{# if (d.status == '-1') { }}
                    {Auth:node name='/manage/admin/Miniapp/install'}
                    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="install">安装</a>
                    {/Auth:node}
                    <!--应用已安装 禁用中-->
                    {{# } else if(d.status == '0') { }}
                    {Auth:node name='/manage/admin/Miniapp/edit'}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">配置</a>
                    {/Auth:node}
                    {Auth:node name='/manage/admin/miniapp/module/index'}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="module">功能</a>
                    {/Auth:node}
                    {Auth:node name='/manage/admin/Miniapp/del'}
<!--                    <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="del">删除</a>-->
                    {/Auth:node}
                    {Auth:node name='/manage/admin/Miniapp/uninstall'}
                    <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="uninstall">卸载</a>
                    {/Auth:node}
                    <!--应用已安装 启用中-->
                    {{# } else if(d.status == '1') { }}
                    {Auth:node name='/manage/admin/Miniapp/edit'}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">配置</a>
                    {/Auth:node}
                    {Auth:node name='/manage/admin/miniapp/module/index'}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="module">功能</a>
                    {/Auth:node}
                    <!--查看客户购买该应用的列表-->
                    {Auth:node name='/manage/admin/MemberMiniapp/index'}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="member">客户</a>
                    {/Auth:node}

                    {{# } else { }}
                    {{# } }}
                </script>

                <!--定制应用-->
                <script type="text/html" id="is_diy">
                    {{# if (d.is_diy == '0') { }}
                    <span style="color: #1E9FFF;">否</span>
                    {{# } else if(d.is_diy == '1') { }}
                    <span style="color: #1E9FFF;">是</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">-</span>
                    {{# } }}
                </script>
            </div>
        </div>
    </div>
    </div>
</div>
{/block}

{block name="js"}
<script>
    var form, table;
    layui.use(['form', 'table', 'bwajax'], function () {
        form = layui.form;
        table = layui.table;
        //执行列表
        table.render({
            elem: '#list',
            url: "{:Url('/manage/admin/Miniapp/index')}",
            response: {
                statusCode: 200,  //规定成功的状态码，默认：0
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.data.total, //解析数据长度
                    "data": res.data.data.list //解析数据列表
                };
            },
            // toolbar: '#listToolbar',
            cols: [[
                // {type:'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', align: 'center', width: 80, fixed: 'left', hide:true},
                {field: 'logo_image', title: 'Logo', align: 'center', width: 80, templet: '#logo_image'},
                {field: 'title', title: '名称', align: 'center', minWidth: 150, templet: '#title'},
                {field: 'types', title: '应用类型', align: 'center', minWidth: 150, templet: '#types'},
                {field: 'is_openapp', title: '接入方式', align: 'center', width: 100, templet: '#is_openapp'},
                // {field: 'sell_price', title: '销售价', align: 'center', width: 80, templet: '#sell_price'},
                {field: 'dir', title: '目录', align: 'center', width: 150, templet: '#dir'},
                {field: 'version', title: '版本', align: 'center', width: 150, templet: '#version'},
                {field: 'status', title: '定制应用', align: 'center', width: 100, templet: '#is_diy'},
                {field: 'status', title: '售卖状态', align: 'center', width: 100, templet: '#status'},
                // {field: 'describe', title: '描述内容', align: 'center', minWidth: 150, templet: '#describe', hide:true},
                // {field: 'qrcode_image', title: '演示二维码', align: 'center', width: 60, templet: '#qrcode_image', hide:true},
                // {field: 'style_images', title: '应用截图', align: 'center', width: 150, templet: '#style_images', hide:true},
                // {field: 'market_price', title: '市场价', align: 'center', width: 80, templet: '#market_price', hide:true},
                // {field: 'template_id', title: '小程序模板', align: 'center', width: 80, templet: '#template_id', hide:true},
                // {field: 'expire_day', title: '体验天数', align: 'center', width: 80, templet: '#expire_day', hide:true},
                // {field: 'is_manage', title: '平台管理', align: 'center', width: 100, templet: '#is_manage', hide:true},
                // {field: 'is_wechat_pay', title: '微信支付', align: 'center', width: 100, templet: '#is_wechat_pay', hide:true},
                // {field: 'is_alipay_pay', title: '支付宝支付', align: 'center', width: 100, templet: '#is_alipay_pay', hide:true},
                // {field: 'sort', title: '排序', align: 'center', width: 80, templet: '#sort', hide:true},
                // {field: 'create_time', title: '安装时间', align: 'center', width: 160, templet: '#create_time', hide:true},
                // {field: 'update_time', title: '更新时间', align: 'center', width: 160, templet: '#update_time', hide:true},
                {title: '操作', toolbar: '#listBar', align: 'center', fixed: "right", width: 160}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            // page: true
        });
        //监听表格头部工具栏事件
        table.on('toolbar(listFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'refresh':
                    reload();
                    break;
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('/manage/admin/Miniapp/add')}",
                    });
                    break;
                case 'del':
                    var checkStatus = table.checkStatus('list');
                    var data = checkStatus.data;
                    var idsArr = [];
                    data.forEach(function(value) {
                        idsArr.push(value.id);
                    });
                    ids = idsArr.join(',');
                    if(!ids) layer.msg('没有选中行', {icon: 2});
                    else del(ids);
                    break;
            }
        });

        //监听工具条
        table.on('tool(listFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            switch (layEvent) {
                case 'edit':
                    layer.open({
                        title: "【" + data.title + "】",
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('/manage/admin/Miniapp/edit')}?id=" + data.id,
                    });
                    break;
                case 'del':
                    del(obj.data.id);
                    break;
                case 'install'://安装
                    install(obj.data.dir);
                    break;
                case 'member'://安装
                    member(obj.data.id);
                    break;
                case 'uninstall':
                    uninstall(obj.data.dir);
                    break;
                case 'module':
                    layer.open({
                        title: "【" + data.title + "】",
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['100%', '90%'],
                        content: "{:Url('/manage/admin/miniapp/module/index')}?miniapp_id=" + data.id,
                    });
                    break;
            }
        });

        form.on('submit(searchBtn)', function (data) {
            reload(1);
            return false;
        })

        //监听switch开关
        form.on('switch(statusChange)', function(obj){
            var state = this.checked ? '1' : '0';

            var id = obj.value;
            bwajax.post("{:Url('/manage/admin/Miniapp/edit')}?id="+id, {id:id,status:state})
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('修改成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                        reload();
                    }
                })
        });
    });

    //表格重载
    function reload(page) {
        var where = getWhere();//获取搜索条件
        var page_now = $(".layui-laypage-em").next().html(); //当前页码值
        var page_to = page ? page : page_now;

        table.reload('list', {
            where: where, //设定异步数据接口的额外参数
            // page: {curr: page_to}//设置页数
        });
    }

    //安装
    function install(dir) {
        layer.confirm('确认安装?', function (index) {
            //ajax调用后台接口
            bwajax.post("{:Url('/manage/admin/Miniapp/install')}?dir=" + dir)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('安装成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

    //安装
    function uninstall(dir) {
        layer.confirm('确认卸载?', function (index) {
            //ajax调用后台接口
            bwajax.post("{:Url('/manage/admin/Miniapp/uninstall')}?dir=" + dir)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('卸载成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

    //查看客户购买该应用的列表
    function member(miniapp_id) {
        layer.open({
            title: '客户应用',
            type: 2,
            shade: 0.2,
            maxmin: true,
            area: ['50%', '90%'],
            content: "/manage/admin/MemberMiniapp/index?miniapp_id=" + miniapp_id,
        });
    }
</script>
{/block}